<?php
namespace Api\Controller;

use Think\Controller;

class CartController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 购物车里的商品
     * @return [type] [description]
     */
    public function index()
    {
        $cart = D('Api/Cart', 'Service')->lists(USER_ID);
        success($cart);
    }

    /**
     * 加入购物车
     */
    public function add()
    {
        $goods_id     = I('post.id', 0, 'intval');
        $spec_item_id = I('post.spec_item_id', 0, 'intval');
        $number       = I('post.number', 0, 'intval');

        $res = D('Api/Cart', 'Service')->add(USER_ID, $goods_id, $spec_item_id, $number);

        if ($res) {
            success();
        } else {
            error();
        }
    }

    /**
     * 删除购物车商品
     * @return [type] [description]
     */
    public function delete()
    {
        $ids = I('post.id', '', 'trim');
        if (!$ids) {
            error(8058, '参数错误');
        }

        $res = D('Api/Cart', 'Service')->delete(USER_ID, $ids);

        if ($res) {
            success();
        } else {
            error(8016, '删除商品失败');
        }
    }

    /**
     * 清空购物车
     * @return [type] [description]
     */
    public function clean()
    {
        $res = D('Api/Cart', 'Service')->clean(USER_ID);

        if ($res) {
            success();
        } else {
            error(8017, '删除商品失败');
        }
    }

    /**
     * 更新购物车
     * @return [type] [description]
     */
    public function update()
    {
        $type    = I('post.type', 'inc', 'trim');
        $cart_id = I('post.cart_id', 0, 'intval');
        $number  = I('post.number', 1, 'intval');

        if (!in_array($type, ['inc', 'dec', 'del'])) {
            error(8018, '操作类型出错');
        }

        switch ($type) {
            case 'inc':
                $res = D('Api/Cart', 'Service')->inc(USER_ID, $cart_id, $number);
                break;
            case 'dec':
                $res = D('Api/Cart', 'Service')->dec(USER_ID, $cart_id, $number);
                break;
            case 'del':
                $res = D('Api/Cart', 'Service')->delete(USER_ID, $cart_id);
                break;
        }

        if ($res) {
            success();
        } else {
            error(8019, '删除商品失败');
        }
    }

    /**
     * 商品数量增加
     * @return [type] [description]
     */
    public function inc()
    {
        $cart_id = I('post.id', 0, 'intval');
        $number  = I('post.number', 1, 'intval');

        $res = D('Api/Cart', 'Service')->inc(USER_ID, $cart_id, $number);

        if ($res) {
            success();
        } else {
            error(8020, '删除商品失败');
        }
    }

    /**
     * 商品数量减少
     * @return [type] [description]
     */
    public function dec()
    {
        $cart_id = I('post.id', 0, 'intval');
        $number  = I('post.number', 1, 'intval');

        $res = D('Api/Cart', 'Service')->dec(USER_ID, $cart_id, $number);

        if ($res) {
            success();
        } else {
            error(8021, '删除商品失败');
        }
    }
}
